package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bo.AnuncioBO;
import bo.InscripcionBO;
import bo.UsuarioBO;
import configuracion.VariablesGlobales;

/**
 * Servlet implementation class FrontController
 */
@SuppressWarnings("serial")
public class  FrontController extends HttpServlet {
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			procesarAccion(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void procesarAccion(HttpServletRequest request, HttpServletResponse response) throws Exception{
			String dir = request.getParameter("dir");
			if(dir==null){
				dir="";
			}
				if(dir.compareTo("sorteo")==0){
					String tipoUsu = (String)request.getSession().getAttribute("tipoUsuario");
					if(tipoUsu!=null && tipoUsu.compareTo("cliente")==0){
						AnuncioBO anuncioBo = new AnuncioBO();
						request.getSession().setAttribute("listaAnuncios", anuncioBo.seleccionaTodos());
						request.getRequestDispatcher(VariablesGlobales.PATH_JSP+"/inscripcionSorteo/menuInscripcion.jsp").forward(request, response);					
					}else{
						request.setAttribute("errorTipoUsuario", "Debe estar logueado como Cliente");
						request.getRequestDispatcher("").forward(request, response);
					}
				}else if(dir.compareTo("anuncio")==0){
					response.sendRedirect(VariablesGlobales.PATH_JSP+"/creacionAnuncios/menuAnuncio.jsp");
				}else if(dir.compareTo("banco")==0){
					response.sendRedirect(VariablesGlobales.PATH_JSP+"/ofertaHipotecaria/tipoInteres.jsp");
				}else if(dir.compareTo("conectar")==0){
					UsuarioBO usuarioBO = new UsuarioBO();
					usuarioBO.conectarUsuario(request, response);
				}else if(dir.compareTo("desconectar")==0){
					UsuarioBO usuarioBO = new UsuarioBO();
					usuarioBO.desconectarUsuario(request, response);
				}else if(dir.compareTo("InscripcionServlet")==0){
					InscripcionBO inscripcionBO = new InscripcionBO();
					inscripcionBO.procesarAccion(request, response);
				}else{
					response.sendRedirect(VariablesGlobales.ROOT_GENERAL+"/index.jsp");
				}	
	}
}
